import axios from 'axios'
import config from './config.js'
import crypt from './crypto-helper-pkcs7'
var httpRequest = {
	GET: function (url, param, success, fail,isEnDomain = false) {
		var paramjson = JSON.stringify(param)
		var params = crypt.encrypt(paramjson)
    console.log(config.domain + url)
    let domain = isEnDomain ? config.endomain : config.domain
		axios.get(domain + url + '?param=' + params)
		.then(res => {
			var response = res.data
			if (response.errcode === 200) {
				typeof success === "function" && success(response.data)
			} else if (response.errcode === 200103) {
				this.$confirm('登录过期，请重新登录')
				.then(_ => {
					window.sessionStorage.clear()
					this.$router.push('/admin/login')
					return false
				})
				.catch(_ => {})
			} else {
				typeof fail === "function" && fail(response)
			}
		})
		.catch(error => {
			console.log(error)
		})
	},
	POST: function (url, param,configs,success, fail,isEnDomain = false) {
    let domain = isEnDomain ? config.endomain : config.domain
		axios.post(domain + url, param, configs)
		.then(res => {
			var response = res.data
			if (response.errcode === 200) {
				typeof success === "function" && success(response.data)
			} else if (response.errcode === 200103) {
				this.$confirm('登录过期，请重新登录')
				.then(_ => {
					window.sessionStorage.clear()
					this.$router.push('/admin/login')
					return false
				})
				.catch(_ => {})
			} else {
				typeof fail === "function" && fail(response)
			}
		})
		.catch(error => {
			console.log(error)
		})
	},
	FORM: function (url, param, configs, success, fail,isEnDomain = false) {
    let domain = isEnDomain ? config.endomain : config.domain
		axios.post(domain + url, param, configs)
		.then(res => {
			var response = res.data
			if (response.errcode === 200) {
				typeof success === "function" && success(response.data)
			} else if (response.errcode === 200103) {
				this.$confirm('登录过期，请重新登录')
				.then(_ => {
					window.sessionStorage.clear()
					this.$router.push('/admin/login')
					return false
				})
				.catch(_ => {})
			} else {
				typeof fail === "function" && fail(response)
			}
		})
		.catch(error => {
			console.log(error)
		})
	},
  DOMAIN:function () {
    return config.domain
  },
  WEB_DOMAIN:function () {
    return config.web_domain
  },
  ENCRYPT:function (data) {
	  let str = typeof (data) === 'string' ? data : JSON.stringify(data)
    return crypt.encrypt(str)
  }
}
export default httpRequest;
